﻿using DevelopTool.Bll.ChatBlls;
using DevelopTool.Common;
using Sunny.UI;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DevelopTool.Forms.BaseForms
{
    /// <summary>
    /// 自定义UIForm基类（UIForm可嵌入到控件中，也能单独弹出窗口）
    /// </summary>
    public partial class BaseUIForm : UIForm
    {
        public BaseUIForm()
        {
            InitializeComponent();
        }

        private void BaseUIForm_Load(object sender,EventArgs e)
        {
        }

        public virtual void ReLoadMenus()
        {

        }

        /// <summary>
        /// 数据验证
        /// </summary>
        /// <param name="control"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        protected bool CheckData(Control control,string msg = null)
        {
            Color defaultColor = Setting.Theme.BackColor;
            Color errorColor = Color.Red;

            if(control is UITextBox textBox)
            {
                if(string.IsNullOrEmpty(textBox.Text))
                {
                    textBox.RectColor = errorColor;
                    if(!string.IsNullOrEmpty(msg))
                        UIMessageTip.ShowWarning(msg,2000,true,PointToScreen(textBox.Location));
                    return false;
                }
                else
                {
                    textBox.RectColor = defaultColor;
                    return true;
                }
            }
            else if(control is UIRichTextBox richTextBox)
            {
                if(string.IsNullOrEmpty(richTextBox.Text))
                {
                    richTextBox.RectColor = errorColor;
                    if(!string.IsNullOrEmpty(msg))
                        UIMessageTip.ShowWarning(msg,2000,true,PointToScreen(richTextBox.Location));
                    return false;
                }
                else
                {
                    richTextBox.RectColor = defaultColor;
                    return true;
                }
            }
            else
            {
                throw new Exception("未知控件类型");
            }
        }


        protected void TextBoxTextChanged(object sender,EventArgs e)
        {
            if(sender is UITextBox textBox)
            {
                if(!string.IsNullOrEmpty(textBox.Text))
                {
                    textBox.RectColor = Setting.Theme.BackColor;
                }
            }
            else if(sender is UIRichTextBox richTextBox)
            {
                if(!string.IsNullOrEmpty(richTextBox.Text))
                {
                    richTextBox.RectColor = Setting.Theme.BackColor;
                }
            }
            else
            {
                throw new Exception("未知控件类型");
            }
        }




        protected void ExitApp()
        {
            ChatNetworkBll.Exit();

            //卸载全局快捷键
            this.UnRegisterGlobalHotKey();           

            //退出吧o(╥﹏╥)o。
            Application.ExitThread();
            Application.Exit();
        }

    }
}
